package com.wl.api.controller.system;

import java.util.function.Predicate;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.OmsProductServiceDTO;
import com.wl.api.dto.system.SecurityChannelPartnerDTO;
import com.wl.api.model.system.OmsProductService;
import com.wl.api.model.system.SecurityChannelPartner;
import com.wl.api.service.business.IOmsProductServiceService;
import com.wl.api.service.system.ISecurityChannelPartnerService;
@RestController
@RequestMapping("/admin/channel/partner")
public class AdminPartnerController extends CommonController {
		@Autowired
		ISecurityChannelPartnerService channelPartnerService;
		@Autowired
		IOmsProductServiceService productService;
		
		@RequestMapping(value="/manage", method=RequestMethod.POST)
		public JsonObject createProdService(@RequestBody SecurityChannelPartnerDTO param) throws Exception {
			JsonObject json = JsonObject.getInstance();
			boolean pass = true;
			String title = "", code = "";
			if (validate().or(s -> s.getPartnerId() != null).test(param)) {
				title = ErrorCodeEnum.param_error.getMessage();
				code = ErrorCodeEnum.param_error.getError_code();
				pass = false;
			}
			if(pass) {
				initCreateUser(param);
				SecurityChannelPartner entity = BeanUtils.copyProperties(param, SecurityChannelPartner.class);
				channelPartnerService.insert(entity);
				json.setData(entity.getPartnerId());
			}
			setJsonMessage(json, title, code, pass);
			return json;
		}
		/**
		 *  修改产品数据
		 * @param param
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/manage", method = RequestMethod.PUT)
		JsonObject modify(@RequestBody SecurityChannelPartnerDTO param) throws Exception {
			JsonObject json = JsonObject.getInstance();
			boolean pass = true;
			String title = "" , error_code = ""; 
			if (validate().and(s -> s.getPartnerId() == null).test(param)) {
				title = ErrorCodeEnum.param_error.getMessage();
				error_code = ErrorCodeEnum.param_error.getError_code();
				pass = false;
			}
			if(pass) {
				initUpdateUser(param);
				SecurityChannelPartner entity = BeanUtils.copyProperties(param, SecurityChannelPartner.class);
				channelPartnerService.updateById(entity);
				json.setData(entity.getPartnerId());
			}
			setJsonMessage(json, title, error_code, pass);
			return json;
		}
		
		@RequestMapping(value="/manage/{id}", method = RequestMethod.DELETE)
		JsonObject del(@PathVariable("id") Integer id) throws Exception {
			JsonObject json = JsonObject.getInstance();
			boolean pass = true;
			String title = "" , error_code = ""; 
			
			if (pass) {
				channelPartnerService.deleteById(id);
			}
			setJsonMessage(json, title, error_code, pass);
			return json;
		}
		
		@RequestMapping(value ="/manage",method = RequestMethod.GET)
		public JsonObject getItemList(SecurityChannelPartnerDTO param) {
			JsonObject json = JsonObject.getInstance();
			param.buildStartTime(param.getStartTime()).buildEndTime(param.getEndTime()).buildPage(param.getPage(), param.getPageSize());
			json.setData(channelPartnerService.selectList(new EntityWrapper<>()));
			setJsonMessage(json);
			return json;
		}  
		
		@RequestMapping(value = "/manage/{id}", method = RequestMethod.GET)
		public JsonObject getDetail(@PathVariable("id")Integer id) {
			JsonObject json = JsonObject.getInstance();
			json.setData(channelPartnerService.selectById(id));
			setJsonMessage(json);
			return json;
		}
		
		private void initCreateUser(SecurityChannelPartnerDTO param) throws Exception {
			CommonUserDTO user = getCommonUser();
			param.buildCreateMsg(user.getId());
		}
		private void initUpdateUser(SecurityChannelPartnerDTO param) throws Exception {
			CommonUserDTO user = getCommonUser();
			param.buildUpdateMsg(user.getUsername());
			
		}
		public Predicate<SecurityChannelPartnerDTO> validate() {
			return  s -> StringUtil.isEmpty(s.getName()) || StringUtil.isEmpty(s.getDesc());
		}
}
